using System;
using System.Collections.Generic;
using System.Text;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace GrapplingHookGameData
{
    public class GameObject
    {
        protected IGameDataCallbacks gameCallback;

        protected ContentManager content;

        protected String textureOrSpritesheetName;
        protected Texture2D texture;
        protected SpriteSheet spriteSheet;
        protected SpriteEffects spriteFlip = SpriteEffects.None;
        protected int currentFrame = 0;
        protected Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        protected Vector2 origin;
        public float Width { get; private set; }
        public float Height { get; private set; }
        protected bool useSpriteSheet;
        protected float rotation = 0;

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }



        public GameObject(IGameDataCallbacks gameCallback, Vector2 startPosition, string textureName)
        {
            this.position = startPosition;
            this.textureOrSpritesheetName = textureName;
            this.useSpriteSheet = false;
            this.gameCallback = gameCallback;
        }

        public GameObject(IGameDataCallbacks gameCallback, Vector2 startPosition, float width, float height, string textureOrSpritesheetName, bool useSpriteSheet)
        {
            Width = width;
            Height = height;
            this.position = startPosition;
            this.textureOrSpritesheetName = textureOrSpritesheetName;
            this.useSpriteSheet = useSpriteSheet;
            this.gameCallback = gameCallback;
        }

        /// <summary>
        /// constructor for no callback
        /// </summary>
        public GameObject(Vector2 startPosition, float width, float height, string textureOrSpritesheetName, bool useSpriteSheet)
            : this(null, startPosition, width, height, textureOrSpritesheetName, useSpriteSheet)
        {}

        /// <summary>
        /// constructor for no callback
        /// </summary>
        public GameObject(Vector2 startPosition, string textureName)
            : this(null, startPosition, textureName)
        { }

        public virtual void LoadContent(ContentManager content)
        {
            this.content = content;

            if (useSpriteSheet)
            {
                spriteSheet = content.Load<SpriteSheet>(textureOrSpritesheetName);
            }
            else
            {
                //load texture and use its width and height if not set previously
                texture = content.Load<Texture2D>(textureOrSpritesheetName);
                if (Width <= 0) Width = texture.Width;
                if (Height <= 0) Height = texture.Height;
            }

            origin = new Vector2(Width / 2f, Height / 2f);
        }

        public virtual void Update(GameTime gameTime, bool isHost)
        {
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (useSpriteSheet)
            {
                Rectangle currentSprite = spriteSheet.SourceRectangle(currentFrame);
                Vector2 origin = spriteSheet.Origins[currentFrame];
                Rectangle destination = new Rectangle((int)Math.Round(Position.X), (int)Math.Round(Position.Y), currentSprite.Width, currentSprite.Height);
                spriteBatch.Draw(spriteSheet.Texture, destination, currentSprite, Color.White, rotation, origin, spriteFlip, 0);
            }
            else
            {
                //Rectangle destination = new Rectangle((int)Math.Round(Position.X), (int)Math.Round(Position.Y), (int)Width, (int)Height);
                //spriteBatch.Draw(texture, destination, null, Color.White, rotation, origin, spriteFlip, 0);
                spriteBatch.Draw(texture, Position, null, Color.White, rotation, origin, 1, spriteFlip, 0);
            }
        }
    }
}
